﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <link href="lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <link href="lib/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script src="lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script src="lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script> 
    <script src="lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
    <script src="lib/ligerUI/js/plugins/ligerTab.js" type="text/javascript"></script>
    <script src="lib/LG.js" type="text/javascript"></script>

   
    <link href="style/css.css" rel="stylesheet" type="text/css" />
</head>
<body style="text-align:center; background:#F0F0F0; overflow:hidden;">
        <div id="topmenu" class="l-topmenu">
            <div class="l-topmenu-logo">Liger.Data代码生成器</div>
            <div class="l-topmenu-welcome"> 
            </div> 
        </div> 
        <div id="layout1" style="text-align:left;width:99.2%; margin:0 auto; margin-top:3px;"> 
            <div position="left" title="数据库" id="layoutleft" style=" overflow:auto;">
                <ul id="dblist"></ul>
            </div>
            <div position="center" id="framecenter"> 
                <div tabid="home" title="我的主页" style="height:300px" id="framecentercontent"> 
                    <h3 style="margin:10px;">欢迎 使用 liger.Data 代码生成工具。 </h3>  
                    <h3 style="margin:10px;">你可以生成整个数据库的文件，也可以生成单个数据库表</h3>
                    <h3 style="margin:10px;">在节点下右击可以操作</h3>
                    <h3 style="margin:10px;">配置参数：</h3>
                    <p style="margin:10px;">命名空间：<input type="text" id="optionNamespace" 
                            class="txtInput" value="Liger.Model" /></p>
                    <p style="margin:10px;">导出目录：<input type="text" id="optionPath" class="txtInput" 
                            value="D:\output" /></p>
                    <p style="margin:10px;  ">是否生成头部版权：<input type="checkbox" id="chkCopyright" checked="checked" class="chkInput" /></p> 

                </div> 

            </div>  
        </div> 
        <div id="dbCreator" style="display:none;">
            <table class="formtable" cellpadding="0" cellspacing="0">
                <tr>
                    <td class="label">链接字符串：</td><td class="inputbody">
                        <textarea id="txtConnectionString">Provider=SQLOLEDB;Data Source=.;Initial Catalog=MyNorthwind;Integrated Security=SSPI</textarea>
                    </td> 
                </tr>
                <tr>
                 <td class="label">数据库选择：</td><td class="inputbody">
                         <select id="dbType">
                            <option value="MSSQL">MSSQL</option>
                            <option value="MSSQL2000">MSSQL2000</option>
                            <option value="Access">Access</option>
                         </select>
                    </td>
                </tr>
            </table>
        </div>
        <div id="dbGenerate" style="display:none;">
            <ul id="listbox1" class="listbox"> 
            </ul>
            <div id="procebtns">
                <a class="procebtn" href="javascript:listboxMove('allTableToRight')">表>></a>
                <a class="procebtn" href="javascript:listboxMove('allTableToLeft')"><<表</a>
                <a class="procebtn" href="javascript:listboxMove('allViewToRight')">视图>></a>
                <a class="procebtn" href="javascript:listboxMove('allViewToLeft')"><<视图</a>
                <a class="procebtn" href="javascript:listboxMove('allToRight')">>></a>
                <a class="procebtn" href="javascript:listboxMove('allToLeft')"><<</a>
                <a class="procebtn" href="javascript:listboxMove('toRight')">></a>
                <a class="procebtn" href="javascript:listboxMove('toLeft')"><</a>
            </div>
            <ul id="listbox2" class="listbox"> 
            </ul>
        </div>
</body>


    <script type="text/javascript">

        var actionNode, winOpenDB, wintip, winGenerate;

        function addTab(title, url)
        {
            LG.showLoading();
            tab.addTabItem({ text: title, url: url, callback: function ()
            {
                LG.hideLoading();
            }
            });
        }


        function menuItemClick(item, i)
        { 
            switch (item.id)
            {
                case "add":
                    treeAdd();
                    break;
                case "load":
                    loadDB(actionNode);
                    break;
                case "generate":
                    generate(actionNode);
                    break;
                case "delete": 
                    deleteTreeNode();
                    break;
                default:
                    break;
            }
        }
         

        function treeAdd()
        {
            if (winOpenDB)
            {
                winOpenDB.show();
            }
            else
            {
                winOpenDB = $.ligerDialog.open({
                    width: 520, isResize: true, isDrag: true, title: '增加数据库',
                    buttons: [
                    { text: '测试连接', onclick: function (item, dialog)
                    {
                        var connstr = $("#txtConnectionString").val();
                        var dbType = $("#dbType").val();
                        LG.callServer({ method: 'TryConnect', data: {
                            dbType: dbType,
                            connectionString: connstr
                        },
                            success: function (data, message)
                            {
                                LG.tip('连接数据库成功!');
                            },
                            error: function (message)
                            {
                                LG.tip('连接数据库失败：' + message);
                            }
                        });
                    }
                    },
                    { text: '确定', onclick: function (item, dialog)
                    { 
                        var connstr = $("#txtConnectionString").val();
                        var dbType = $("#dbType").val();
                        var targetData = actionNode;
                        LG.callServer({ method: 'GetDb', data: {
                            dbType: dbType,
                            connectionString: connstr
                        },
                            success: function (data)
                            {
                                appendDb(data, targetData);
                                winOpenDB.hide();
                            },
                            error: function (message)
                            {
                                LG.tip('连接数据库失败：' + message);
                            }
                        });
                    }
                    },
                    { text: '取消', onclick: function (item, dialog) { dialog.hide(); } }
                    ],
                    target: $('#dbCreator')
                });
            }
        }
        function deleteTreeNode() {
            if (!actionNode) return;
            LG.callServer({ method: 'RemoveDbSetting', data: { 
                connectionString: actionNode.connectionString
            },
            success: function (id) {
                    tree.remove(actionNode);
                    LG.tip('已删除数据库节点');
                },
                error: function (message) {
                    LG.tip('删除数据库节点失败：' + message);
                }
            });
        }
        var menu = $.ligerMenu({ top: 100, left: 100, width: 120, items:
            [
                { text: '增加', click: menuItemClick, id: 'add', icon: 'add' },
                { text: '生成代码', icon: 'generate', children: [
                    { text: '实体', click: menuItemClick, id: 'generate', icon: 'generate' }
                ]
                },
                { text: '加载', click: menuItemClick, id: 'load', icon: 'load' },
            //{ line: true },
                {text: '删除', click: menuItemClick, id: 'delete', icon: 'delete' }
            ]
        });
        function setMenuClickIcons(icons)
        { 
            icons = icons || [];
            menu.setDisabled('add');
            menu.setDisabled('generate');
            menu.setDisabled('delete');
            menu.setDisabled('load'); 
            for (var i in icons)
            {
                menu.setEnabled(icons[i]); 
            }
        }

        //Checkbox
        $(":checkbox").ligerCheckBox();

        //Layout
        var layout = $("#layout1").ligerLayout({ height: '100%', heightDiff: -3,leftWidth: 210, onHeightChanged: function (e)
            { 
                $("#layoutleft").height(e.middleHeight - 25);
            }
        });
        var height = $(".l-layout-center").height();
        $("#layoutleft").height(height - 25);
        

        //Tab
        var tab = $("#framecenter").ligerTab({ height: height });
        $("#framecentercontent").removeAttr("title");

        var rootData = { text: '数据库', type: 'root', icon: 'images/folder_table.png' };

        //tree
        var tree = $("#dblist").ligerTree({
            checkbox: false,
            data: [rootData], nodeWidth: 200, slide: false,btnClickToToggleOnly :false,
            onContextmenu: function (node, e)
            {
                actionNode = node.data;
                menu.show({ top: e.pageY, left: e.pageX });
                switch (node.data.type)
                {
                    case "root":
                        setMenuClickIcons(['add']);
                        break;
                    case "db":
                        setMenuClickIcons(['generate', 'delete']);
                        break;
                    case "tables": 
                    case "views":
                        setMenuClickIcons(['load']);
                        break;
                    case "table":
                    case "view":
                        setMenuClickIcons(['generate', 'load']);
                        break;
                    default:
                        setMenuClickIcons();
                        break;
                }
                return false;
            }
        });

        function loadSettings() {
            LG.callServer({ method: 'GetSettings', loading: '正在加载配置文件...',
                success: function (data)
                {
                    if (data instanceof Array && data.length)
                    {
                        var nodes = [];
                        for (var i in data)
                        {
                            var o = data[i];
                            var newnode = { type: 'db', name: o.name, text: o.name, dbType: o.dbType, connectionString: o.connectionString, icon: data.dbType == "Access" ? 'images/database.png' : 'images/database_yellow.png', children: [] };
                            newnode.children.push({ text: '表', type: 'tables', dbType: o.dbType, connectionString: o.connectionString, icon: 'images/folder.png' });
                            newnode.children.push({ text: '视图', type: 'views', dbType: o.dbType, connectionString: o.connectionString, icon: 'images/folder.png' });
                            nodes[nodes.length] = newnode;

                        }
                        if (nodes.length > 0)
                        {
                            tree.append(rootData, nodes);
                            LG.tip('加载配置文件成功,节点数：' + nodes.length);
                        }
                    }
                },
                error: function (message)
                {
                    LG.tip('加载配置文件失败：' + message);
                }
            });
        }
        loadSettings();

        function appendDb(data,targetData) {
            //先调用后台方法加入配置文件，成功以后才增加到树
            LG.callServer({ method: 'AddDbSetting', data: {
                dbType: data.dbType,
                connectionString: data.connectionString
            },
                success: function (id)
                {
                    var treeNewData = {
                        text: data.name,
                        name: data.name,
                        type: 'db',
                        dbType: data.dbType,
                        connectionString: data.connectionString,
                        icon: data.dbType == "Access" ? 'images/database.png' : 'images/database_yellow.png',
                        children: [
                            { text: '表', type: 'tables', dbType: data.dbType, connectionString: data.connectionString, icon: 'images/folder.png' },
                            { text: '视图', type: 'views', dbType: data.dbType, connectionString: data.connectionString, icon: 'images/folder.png' }
                        ]
                    };

                    tree.append(targetData, [treeNewData]);
                    LG.tip('增加成功');
                },
                error: function (message)
                {
                    LG.tip('增加失败：' + message);
                }
            });
        }
        function generate(data)
        { 
            if (!data) return;
            if (data.type == "db")
            {
                $(".listbox").html("");
                loadDB(actionNode.children[0], function (tables)
                {
                    $(tables).each(function ()
                    {
                        $("#listbox1").append("<li class='table'>[表]" + this.name + "</li>");
                    });
                    if (actionNode.children.length <= 1) return;
                    loadDB(actionNode.children[1], function (views)
                    {
                        $(views).each(function ()
                        {
                            $("#listbox1").append("<li class='view'>[视图]" + this.name + "</li>");
                        });
                    });
                }); 
                if (winGenerate)
                {
                    winGenerate.show();
                }
                else
                {
                    winGenerate = $.ligerDialog.open({
                        width: 500, isResize: true, isDrag: true, title: '生成代码',
                        buttons: [
                    { text: '确定', onclick: function (item, dialog)
                    {
                        if (LG.loading) return;
                        var include = listboxGetSelected();
                        if (include == "")
                        {
                            LG.tip('请选择表/视图');
                            return;
                        }
                        var path = $("#optionPath").val();
                        var namespace = $("#optionNamespace").val();
                        LG.callServer({ method: 'Generate', loading: '正在生成文件...',
                            data: {
                                dbType: data.dbType,
                                connectionString: data.connectionString,
                                path: path,
                                include: include,
                                copyright: $("#chkCopyright").is(":checked"),
                                space: namespace
                            },
                            success: function (data)
                            {
                                LG.tip('生成代码成功');
                                dialog.hide();
                            },
                            error: function (message)
                            {
                                LG.tip('生成代码失败：' + message);
                            }
                        });
                    }
                    },
                    { text: '取消', onclick: function (item, dialog) { dialog.hide(); } }
                    ],
                        target: $('#dbGenerate')
                    });
                }
            }
            else if (data.type == "table" || data.type == "view")
            {
                var ajaxData = {
                    dbType: data.dbType,
                    connectionString: data.connectionString ,
                    Namespace: $("#optionNamespace").val()
                };
                if (data.type == "table") ajaxData.table = data.name;
                else ajaxData.view = data.name;
                var url = "Code.aspx?" + $.param(ajaxData);
                addTab(data.name, url);
            }
        }
        //双击：加载数据
        function loadDB(data,callback) {
            if (!data) return; 
            if (data.type == "tables" || data.type == "views")
            {
                if (!data.loaded || callback)
                {
                    var methodName = data.type == "tables" ? 'GetTables' : 'GetViews';
                    LG.callServer({
                        method: methodName, loading: '正在加载' + (data.type == "views" ? '视图' : '表') + '...',
                        data: {
                            dbType: data.dbType,
                            connectionString: data.connectionString
                        },
                        success: function (nodes, message)
                        {
                            if (callback)
                            {
                                callback(nodes, message);
                            }
                            else
                            {
                                tree.append(data, nodes);
                                data.loaded = true;
                            }
                        },
                        error: function (message)
                        {
                            LG.tip('连接数据库失败：' + message);
                        }
                    });
                }
            }
            else if (data.type == "table" || data.type == "view")
            {
                var methodName = data.type == "table" ? 'GetColumns' : 'GetViewColumns';
                var ajaxData = {
                    dbType: data.dbType, 
                    connectionString: data.connectionString
                };
                if (data.type == "table") ajaxData.table = data.name;
                else ajaxData.view = data.name;
                if (!data.loaded || callback)
                {
                    LG.callServer({
                        method: methodName,
                        data: ajaxData,
                        success: function (columns, message)
                        {
                            if (callback)
                            {
                                callback(nodes, message);
                            }
                            else
                            {
                                tree.append(data, columns);
                                data.loaded = true;
                            }
                        },
                        error: function (message)
                        {
                            LG.tip('连接数据库失败：' + message);
                        }
                    });
                }
            }
        }

        function listboxinit()
        {
            $("#procebtns a.procebtn").hover(function ()
            {
                $(this).addClass("procebtn-over");
            }, function ()
            {
                $(this).removeClass("procebtn-over");
            });
            $(".listbox li").live('click',function ()
            {
                if($(this).hasClass("selected"))
                    $(this).removeClass("selected");
                else
                    $(this).addClass("selected");
            });
        }
        function listboxGetSelected()
        {
            var include = "";
            var appended = false;
            $("#listbox2 li").each(function ()
            {
                if (appended) include += ",";
                var name = $(this).html();
                name = name.replace("[表]", "").replace("[视图]", "");
                include += name;
                appended = true;
            });
            return include;
        }
        function listboxMove(type)
        {
            switch (type)
            {
                case "toRight":
                    $("#listbox1 li.selected").appendTo($("#listbox2")).removeClass("selected");
                    break;
                case "toLeft":
                    $("#listbox2 li.selected").appendTo($("#listbox1")).removeClass("selected");
                    break;
                case "allToRight": 
                    $("#listbox1 li").appendTo($("#listbox2")).removeClass("selected");
                    break;
                case "allToLeft": 
                    $("#listbox2 li").appendTo($("#listbox1")).removeClass("selected");
                    break;
                case "allTableToRight":
                    $("#listbox1 li.table").appendTo($("#listbox2")).removeClass("selected");
                    break;
                case "allTableToLeft":
                    $("#listbox2 li.table").appendTo($("#listbox1")).removeClass("selected");
                    break;
                case "allViewToRight":
                    $("#listbox1 li.view").appendTo($("#listbox2")).removeClass("selected");
                    break;
                case "allViewToLeft":
                    $("#listbox2 li.view").appendTo($("#listbox1")).removeClass("selected");
                    break;
                default:
                    break;
            }
        }
        listboxinit();
         
       
        $("#dblist").dblclick(function (e)
        {
            var obj = (e.target || e.srcElement);
            var treeitem = null;
            if (obj.tagName.toLowerCase() == "a" || obj.tagName.toLowerCase() == "span" || $(obj).hasClass("l-box"))
                treeitem = $(obj).parent().parent();
            else if ($(obj).hasClass("l-body"))
                treeitem = $(obj).parent();
            else
                treeitem = $(obj);
            if (!treeitem) return;
            var treedataindex = parseInt(treeitem.attr("treedataindex"));
            var treenodedata = tree._getDataNodeByTreeDataIndex(tree.data, treedataindex);
            loadDB(treenodedata);
        });

        $(document).bind('contextmenu', function ()
        {
            return false; 
        });
        $(document).bind("selectstart", function (e) {
            var obj = (e.target || e.srcElement);
            var tagName = obj.tagName.toLowerCase();
            if (tagName == "input" || tagName == "textarea") return true;
            return false;
        });
    </script>
</html>
